package com.yuexun.dhdoctor.module.Contract.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseEntity;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.Contract.adapter.CreateGroupAdapter;
import com.yuexun.dhdoctor.module.Contract.bean.CreateGroup;
import com.yuexun.dhdoctor.module.Contract.bean.DepartOrPeopleData;
import com.yuexun.dhdoctor.module.Contract.bean.GoupExist;
import com.yuexun.dhdoctor.module.Contract.bean.People;
import com.yuexun.dhdoctor.module.Contract.model.impl.CreateGroupModel;
import com.yuexun.dhdoctor.module.Contract.presenter.impl.CreateGroupPrensenter;
import com.yuexun.dhdoctor.module.Contract.ui.view.ICreateGroupView;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by work-jx on 2016/7/22.
 */
public class Act_CreateGroup extends BaseKJActivity implements ICreateGroupView {

    @Bind(R.id.title_tv)
    TextView titleTv;
    @Bind(R.id.creategroup_name_edt)
    EditText creategroupNameEdt;
    @Bind(R.id.grid)
    GridView grid;
    @Bind(R.id.creategroup_count)
    TextView creategroupcount;

    ArrayList<People> peoples = new ArrayList<People>();
    CreateGroupAdapter adapter;

    private CreateGroupPrensenter presenter;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_creategroup);
        ButterKnife.bind(this);
        setGridView();
        presenter = new CreateGroupPrensenter(new CreateGroupModel(),this);
    }

    private void setGridView() {

        int size = peoples.size();

        int length = 80;

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        float density = dm.density;
        int gridviewWidth = (int) (size * (length + 4) * density);
        int itemWidth = (int) (length * density);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                gridviewWidth, LinearLayout.LayoutParams.FILL_PARENT);
        grid.setLayoutParams(params); // 重点
        grid.setColumnWidth(itemWidth); // 重点
        grid.setHorizontalSpacing(5); // 间距
        grid.setStretchMode(GridView.NO_STRETCH);
        grid.setNumColumns(size); // 重点

        List<DepartOrPeopleData> datas = new ArrayList<>();
        for(People people :peoples){
            DepartOrPeopleData data = new DepartOrPeopleData();
            data.setDataId(people.getUserId() + "");
            data.setDataName(people.getUserName());
            datas.add(data);
        }

        adapter = new CreateGroupAdapter(grid,datas,R.layout.item_send_addmember);
        grid.setAdapter(adapter);

        creategroupcount.setText(size+"人");
    }


    @OnClick({R.id.back, R.id.creategroup_save, R.id.group_add, R.id.group_delete})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.creategroup_save:
                Map<String, String> params = new HashMap<>();
                params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
                params.put("accountId", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.ACCOUNT_ID));
                params.put("title",creategroupNameEdt.getText().toString());
                params.put("groupType","1");
                presenter.isGroupExisit(params);
                break;
            case R.id.group_add:
                Bundle bundle = new Bundle();
                Intent intent = new Intent(this, Act_AddMember.class);
                bundle.putSerializable("persons", peoples);
                intent.putExtras(bundle);
                aty.startActivityForResult(intent, 1);
                break;
            case R.id.group_delete:
                Bundle bundle_delete = new Bundle();
                Intent intent_delete = new Intent(this, Act_DeleteMember.class);
                bundle_delete.putSerializable("persons", peoples);
                intent_delete.putExtras(bundle_delete);
                aty.startActivityForResult(intent_delete, 2);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case RESULT_OK:
//                peoples.removeAll(peoples);
//                setGridView();
                peoples = (ArrayList<People>) data.getExtras().getSerializable("persons");
                setGridView();
                break;
            case 101:
//                peoples.removeAll(peoples);
//                setGridView();
                peoples = (ArrayList<People>) data.getExtras().getSerializable("persons");
                setGridView();
                break;
            default:
                break;
        }
    }

    @Override
    public void showCreateGroup(CreateGroup createGroup) {
        if("200".equals(createGroup.getStatus()))
        {
            ViewInject.toast("群组创建成功");
            finish();
        }
    }

    @Override
    public void showGroup(BaseEntity<GoupExist> entity) {
        if(entity.getStatus().equals("200")){
            List<GoupExist> strings = entity.getDatas();
            if(strings.get(0).getGroupNameExist().equals("1")){
                String memberIds=null,names=null;

                for(int i =0;i<peoples.size();i++)
                {
                    if(i==0)
                    {
                        memberIds = peoples.get(i).getUserId();
                        names = peoples.get(i).getUserName();
                    }
                    else {
                        memberIds = memberIds+";"+peoples.get(i).getUserId();
                        names = names+";"+peoples.get(i).getUserName();
                    }
                }

                Map<String, String> params = new HashMap<>();
                params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
                params.put("accountId", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.ACCOUNT_ID));
                params.put("title",creategroupNameEdt.getText().toString());
                params.put("name", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.USER_NAME));
                params.put("memberIds",memberIds);
                params.put("names",names);
                presenter.createGroup(params);
            }else{
                ViewInject.toast("群组名称已存在");
            }
        }

    }

}
